//
//  XLPasswordView.m
//  industry
//
//  Created by xl on 2020/12/28.
//  Copyright © 2020 Haier. All rights reserved.
//

#import "XLPasswordView.h"

@interface XLPasswordView ()

@property (nonatomic,strong) NSMutableString *textStore;

@end
@implementation XLPasswordView

static NSString  * const MONEYNUMBERS = @"0123456789";

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.textStore = [NSMutableString string];
        self.squareWidth = frame.size.height - 2;
        self.passWordNum = 4;
        self.pointRadius = 6;
        self.rectColor = [UIColor colorWithHex:0xE1E1E1];
        self.pointColor = [UIColor blackFontColor];
        [self becomeFirstResponder];
    }
    
    return  self;
}

/**
 * 设置密码框边长
 */
-(void)setSquareWidth:(CGFloat)squareWidth
{
    _squareWidth = squareWidth;
    [self setNeedsDisplay];
}

/**
 * 设置键盘类型
 */

-(UIKeyboardType)keyboardType
{
    return UIKeyboardTypeNumberPad;
}

/**
 *  设置密码位数
 *
 */

-(void)setPassWordNum:(NSInteger)passWordNum
{
    _passWordNum = passWordNum;
    [self setNeedsDisplay];
}

-(BOOL)becomeFirstResponder
{
    if ([self.delegate respondsToSelector:@selector(passWordBeginInput:)]) {
        [self.delegate passWordBeginInput:self];
    }
    
    return [super becomeFirstResponder];
}

/**
 *  是否成为第一响应者
 *
 */
-(BOOL)canBecomeFirstResponder
{
    return YES;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    if (![self isFirstResponder]) {
        [self becomeFirstResponder];
    }
}

#pragma mark - UIKeyInput
/**
 * 用于显示的文本对象是否有任何文本
 */
-(BOOL)hasText
{
    return self.textStore.length > 0;
}

/**
 *插入文本
 */
-(void)insertText:(NSString *)text
{
    if (self.textStore.length < self.passWordNum) {
        
        //判断是否有数字
        NSCharacterSet *character = [[NSCharacterSet characterSetWithCharactersInString:MONEYNUMBERS] invertedSet];
        
        NSString *filter = [[text componentsSeparatedByCharactersInSet:character] componentsJoinedByString:@""];

        BOOL basicTest = [text isEqualToString:filter];
        if (basicTest) {
            [self.textStore appendString:text];
            if ([self.delegate respondsToSelector:@selector(passWordDidChange:)]) {
                [self.delegate passWordDidChange:self];
            }
            
            if (self.textStore.length == self.passWordNum) {
                if ([self.delegate respondsToSelector:@selector(passWordCompleteInput:)]) {
                    [self.delegate passWordCompleteInput:self];
                }
            }
            [self setNeedsDisplay];
            
        }
        
    }
}

//删除文本
-(void)deleteBackward
{
    if (self.textStore.length > 0) {
        [self.textStore deleteCharactersInRange:NSMakeRange(self.textStore.length - 1, 1)];
        if ([self.delegate respondsToSelector:@selector(passWordDidChange:)]) {
            [self.delegate passWordDidChange:self];
        }
    }
    
    [self setNeedsDisplay];
}

-(void)drawRect:(CGRect)rect
{
    CGFloat height = rect.size.height;
    CGFloat width = rect.size.width;
    
    CGFloat margin_x = 30;//X起始位置
    CGFloat x = (width - self.squareWidth * self.passWordNum - 2*margin_x)/((CGFloat)self.passWordNum - 1);
    CGFloat y = (height - self.squareWidth)/2.0;
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    //画外框
    CGContextSetLineWidth(context, 0.5);
    CGContextSetStrokeColorWithColor(context, self.rectColor.CGColor);
    //画竖条
    for (int i = 0; i < self.passWordNum; i ++) {
        CGContextMoveToPoint(context, margin_x + x * i + (i + 1)*self.squareWidth, y + self.squareWidth - 10);
        CGContextAddArcToPoint(context, margin_x + x * i + (i + 1)*self.squareWidth, y + self.squareWidth, margin_x + x * i + (i + 1)*self.squareWidth - 10, y + self.squareWidth, 5);//右下角
        CGContextAddArcToPoint(context, margin_x + (x + self.squareWidth) * i, y + self.squareWidth, margin_x + (x + self.squareWidth) * i, y + self.squareWidth - 10, 5);//左下角
        CGContextAddArcToPoint(context, margin_x + (self.squareWidth + x) * i, y, margin_x + (x + self.squareWidth) * i + 10, y, 5);//左上角
        CGContextAddArcToPoint(context, margin_x + x * i + (i + 1)*self.squareWidth, y, margin_x + x * i + (i + 1)*self.squareWidth, y + 10, 5);//右上角
        CGContextClosePath(context);
    }
    
    CGContextDrawPath(context, kCGPathStroke);
    CGContextSetFillColorWithColor(context, self.pointColor.CGColor);
    
    //画黑点
    for (int i = 0; i < self.textStore.length; i++) {
        CGContextAddArc(context, margin_x + i * (x + self.squareWidth) + self.squareWidth/2.0, y + self.squareWidth/2.0, self.pointRadius, 0, M_PI * 2, YES);
        CGContextDrawPath(context, kCGPathFill);
    }
}

#pragma mark - Public
- (void)clearCurrentPassword {
    if (self.textStore.length > 0) {
        [self.textStore deleteCharactersInRange:NSMakeRange(0, self.textStore.length)];
    }
    [self setNeedsDisplay];
}

@end
